package com.ssy.lingxi.product.controller.pc;

import com.ssy.lingxi.common.constant.manage.ShopTypeEnum;
import com.ssy.lingxi.common.constant.member.MemberLevelTypeEnum;
import com.ssy.lingxi.common.constant.product.CommodityStatusEnum;
import com.ssy.lingxi.common.constant.product.PriceTypeEnum;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.commodity.*;
import com.ssy.lingxi.product.api.model.response.commodity.CommodityGoodsResponse;
import com.ssy.lingxi.product.api.model.response.commodity.CommodityStockResponse;
import com.ssy.lingxi.product.api.model.response.commodity.CommonCommodityPlatformResponse;
import com.ssy.lingxi.product.service.IChannelCommodityService;
import com.ssy.lingxi.product.service.ICommodityService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @menu: 商品公共接口
 * 商品公共接口类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/28
 */
@RestController
@RequestMapping("/product/commodity/common")
public class CommodityCommonController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private IChannelCommodityService channelCommodityService;

    /**
     * 查询商品列表--代客下单
     * @param pageVO 分页实体
     * @param commodityGuestRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityListByGuest", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityGoodsResponse>> getCommodityListByGuest(PageVO pageVO, CommodityGuestRequest commodityGuestRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        CommodityDetailRequest commodityDetailRequest = this.modelMapper.map(commodityGuestRequest, CommodityDetailRequest.class);

        //商品状态
        List<Integer> statusList = new ArrayList<>();
        statusList.add(CommodityStatusEnum.On_Shelf.getCode());
        commodityDetailRequest.setStatusList(statusList);

        //供应商
        commodityDetailRequest.setMemberId(sysUser.getMemberId());
        //采购商
        commodityDetailRequest.setChildMemberId(commodityGuestRequest.getMemberId());

        //商城id
        Long shopId = commodityGuestRequest.getShopId();
        if(shopId != null && shopId > 0){
            List<Long> shopIdList = new ArrayList<>();
            shopIdList.add(shopId);
            commodityDetailRequest.setShopIdList(shopIdList);
        }
        //商城类型
        Integer memberLevelType = sysUser.getMemberLevelType();
        if(MemberLevelTypeEnum.CHANNEL.getCode().equals(memberLevelType)){
            return Wrapper.success(channelCommodityService.getCommodityDetailList(pageVO, commodityDetailRequest, sysUser.getMemberId()));
        }else{
            return Wrapper.success(commodityService.getCommodityDetailList(pageVO, commodityDetailRequest, sysUser.getMemberId(), sysUser.getMemberRoleId()));
        }
    }

    /**
     * 查询商品列表--采购商角色(买方)
     * @param pageVO 分页实体
     * @param commodityBuyerRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityListByBuyer", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityGoodsResponse>> getCommodityListByBuyer(PageVO pageVO, CommodityBuyerRequest commodityBuyerRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        CommodityDetailRequest commodityDetailRequest = this.modelMapper.map(commodityBuyerRequest, CommodityDetailRequest.class);
        //商品状态
        List<Integer> statusList = new ArrayList<>();
        statusList.add(CommodityStatusEnum.On_Shelf.getCode());
        commodityDetailRequest.setStatusList(statusList);
        //供应商会员id
        commodityDetailRequest.setMemberId(commodityBuyerRequest.getMemberId());
        //当前登录用户id
        commodityDetailRequest.setChildMemberId(sysUser.getMemberId());
        //商城id
        Long shopId = commodityBuyerRequest.getShopId();
        if(shopId != null && shopId > 0){
            List<Long> shopIdList = new ArrayList<>();
            shopIdList.add(shopId);
            commodityDetailRequest.setShopIdList(shopIdList);
        }

        //商城类型
        Integer memberLevelType = sysUser.getMemberLevelType();
        if(MemberLevelTypeEnum.CHANNEL.getCode().equals(memberLevelType)){
            return Wrapper.success(channelCommodityService.getCommodityDetailList(pageVO, commodityDetailRequest, sysUser.getMemberId()));
        }else{
            return Wrapper.success(commodityService.getCommodityDetailList(pageVO, commodityDetailRequest, sysUser.getMemberId(), sysUser.getMemberRoleId()));
        }
    }

    /**
     * 查询商品列表--供应商角色(卖方)
     * @param pageVO 分页实体
     * @param commoditySellerRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityListBySeller", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityGoodsResponse>> getCommodityListBySeller(PageVO pageVO, CommoditySellerRequest commoditySellerRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        CommodityDetailRequest commodityDetailRequest = this.modelMapper.map(commoditySellerRequest, CommodityDetailRequest.class);

        //商品状态
        List<Integer> statusList = commoditySellerRequest.getStatusList();
        if (statusList == null || statusList.size() <= 0) {
            statusList = new ArrayList<>();
            statusList.add(CommodityStatusEnum.On_Shelf.getCode());
        }else{
            commodityDetailRequest.setShopType(null);
            commodityDetailRequest.setEnvironment(null);
        }
        commodityDetailRequest.setStatusList(statusList);

        //供应商
        commodityDetailRequest.setMemberId(sysUser.getMemberId());

        //商城id
        Long shopId = commoditySellerRequest.getShopId();
        if(shopId != null && shopId > 0){
            List<Long> shopIdList = new ArrayList<>();
            shopIdList.add(shopId);
            commodityDetailRequest.setShopIdList(shopIdList);
        }
        //商城类型
        Integer memberLevelType = sysUser.getMemberLevelType();
        if(MemberLevelTypeEnum.CHANNEL.getCode().equals(memberLevelType)){
            return Wrapper.success(channelCommodityService.getCommodityDetailList(pageVO, commodityDetailRequest, sysUser.getMemberId()));
        }else{
            return Wrapper.success(commodityService.getCommodityDetailList(pageVO, commodityDetailRequest, sysUser.getMemberId(), sysUser.getMemberRoleId()));
        }
    }

    /**
     * 查询商品列表--平台(全部商品)
     * @param commodityPlatformRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityListByPlatform", method = RequestMethod.POST)
    public Wrapper<PageData<CommonCommodityPlatformResponse>> getCommodityListByPlatform(@RequestBody CommonCommodityPlatformRequest commodityPlatformRequest) {
        CommodityDetailRequest commodityDetailRequest = this.modelMapper.map(commodityPlatformRequest, CommodityDetailRequest.class);
        //商品状态
        List<Integer> statusList = new ArrayList<>();
        statusList.add(CommodityStatusEnum.On_Shelf.getCode());
        commodityDetailRequest.setStatusList(statusList);
        //商城id
        Long shopId = commodityPlatformRequest.getShopId();
        if(shopId != null && shopId > 0){
            List<Long> shopIdList = new ArrayList<>();
            shopIdList.add(shopId);
            commodityDetailRequest.setShopIdList(shopIdList);
        }
        //分页参数
        PageVO pageVO = new PageVO();
        pageVO.setCurrent(commodityPlatformRequest.getCurrent());
        pageVO.setPageSize(commodityPlatformRequest.getPageSize());
        return Wrapper.success(commodityService.getCommodityListByPlatform(pageVO, commodityDetailRequest));
    }

    /**
     * 查询商品列表--库存设置
     * @param pageVO 分页实体
     * @param commodityStockRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityListByStock", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityStockResponse>> getCommodityListByStock(PageVO pageVO, CommodityStockRequest commodityStockRequest) {
        Integer shopType = commodityStockRequest.getShopType();
        Long brandId = commodityStockRequest.getBrandId();
        Long customerCategoryId = commodityStockRequest.getCustomerCategoryId();
        String name = commodityStockRequest.getName();
        //构建查询参数
        CommodityDetailRequest commodityDetailRequest = new CommodityDetailRequest();
        commodityDetailRequest.setBrandId(brandId);
        commodityDetailRequest.setCustomerCategoryId(customerCategoryId);
        commodityDetailRequest.setName(name);
        //会员信息
        UserLoginCacheDTO sysUser = this.getSysUser();
        commodityDetailRequest.setMemberId(sysUser.getMemberId());
        commodityDetailRequest.setMemberRoleId(sysUser.getMemberRoleId());

        List<Integer> statusList = new ArrayList<>();
        List<Integer> priceTypeList = new ArrayList<>();
        if(ShopTypeEnum.ENTERPRISE.getCode().equals(shopType)){
            //商品状态
            statusList.add(CommodityStatusEnum.Pass.getCode());
            statusList.add(CommodityStatusEnum.On_Shelf.getCode());
            statusList.add(CommodityStatusEnum.Off_Shelf.getCode());
            statusList.add(CommodityStatusEnum.NEVER_Shelf.getCode());
            commodityDetailRequest.setStatusList(statusList);
            //价格类型
            priceTypeList.add(PriceTypeEnum.Cash.getCode());
            priceTypeList.add(PriceTypeEnum.Consult.getCode());
            priceTypeList.add(PriceTypeEnum.Gift.getCode());
            commodityDetailRequest.setPriceTypeList(priceTypeList);
            return Wrapper.success(commodityService.getCommodityDetailStockList(pageVO, commodityDetailRequest, sysUser.getMemberRoleType()));
        }else if(ShopTypeEnum.SCORE.getCode().equals(shopType)){
            //商品状态
            statusList.add(CommodityStatusEnum.Pass.getCode());
            statusList.add(CommodityStatusEnum.On_Shelf.getCode());
            statusList.add(CommodityStatusEnum.Off_Shelf.getCode());
            statusList.add(CommodityStatusEnum.NEVER_Shelf.getCode());
            commodityDetailRequest.setStatusList(statusList);
            //价格类型
            priceTypeList.add(PriceTypeEnum.Score.getCode());
            commodityDetailRequest.setPriceTypeList(priceTypeList);
            return Wrapper.success(commodityService.getCommodityDetailStockList(pageVO, commodityDetailRequest, sysUser.getMemberRoleType()));
        }else if(ShopTypeEnum.CHANNEL.getCode().equals(shopType) || ShopTypeEnum.CHANNEL_SELF.getCode().equals(shopType)){
            //商品状态
            statusList.add(CommodityStatusEnum.On_Shelf.getCode());
            commodityDetailRequest.setStatusList(statusList);
            //价格类型
            priceTypeList.add(PriceTypeEnum.Cash.getCode());
            priceTypeList.add(PriceTypeEnum.Gift.getCode());
            commodityDetailRequest.setPriceTypeList(priceTypeList);
            return Wrapper.success(channelCommodityService.getCommodityDetailStockList(pageVO, commodityDetailRequest));
        }else if(ShopTypeEnum.CHANNEL_SCORE.getCode().equals(shopType)){
            //商品状态
            statusList.add(CommodityStatusEnum.On_Shelf.getCode());
            commodityDetailRequest.setStatusList(statusList);
            //价格类型
            priceTypeList.add(PriceTypeEnum.Score.getCode());
            commodityDetailRequest.setPriceTypeList(priceTypeList);
            return Wrapper.success(channelCommodityService.getCommodityDetailStockList(pageVO, commodityDetailRequest));
        }else{
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }
    }

}
